<script setup>
import AsideCom from '@/components/AsideCom.vue';
import { ref, watch } from 'vue';
import { Fold, Expand } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/user'
import { ElMessage, ElMessageBox } from 'element-plus'
import { logoutFn } from '@/api/user'

// 是否展开侧边栏
const collapse = ref(false)
const width = ref('200px')
const headerLeft = ref(Fold)

// 创建router
const router = useRouter()

const userStore = useUserStore()

// 监听是否展开侧边栏
watch(collapse, (newValue) => {
  if (newValue) {
    width.value = "64px"
    headerLeft.value = Expand 
  } else {
    width.value = "200px"
    headerLeft.value = Fold
  }
}, { immediate: true }) 

//登录
function loginClick(){
  router.push('/login')
}

// 跳转到登录页
const goToLogin = () => {
  router.push('/login')
}

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(async () => {
    try {
      await logoutFn()
      userStore.clearUserInfo()
      ElMessage.success('退出登录成功')
      router.push('/login')
    } catch (error) {
      console.error('退出登录失败:', error)
      ElMessage.error('退出登录失败')
    }
  }).catch(() => {})
}
</script>

<template>
  <div class="common-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="width">
        <h5 class="mb-2" v-if="!collapse">运营管理系统</h5>
        <h5 v-else></h5>
        <!-- 侧边栏组件 -->
        <AsideCom :collapse="collapse"/>
      </el-aside>
      
      <el-container>
        <!-- 头部 -->
        <el-header>
          <el-icon size="30" color="white" @click="collapse = !collapse">
            <component :is="headerLeft"></component>
          </el-icon>
          <div>
            <div v-if="userStore.adminname" class="user-info">
              <span style="color: white;padding-right: 5px;">欢迎，{{ userStore.adminname }}</span>
              <el-button type="danger" size="small" @click="handleLogout">退出登录</el-button>
            </div>
            <el-button v-else type="primary" @click="goToLogin">登录</el-button>
          </div>
        </el-header>
        <!-- 内容 -->
        <el-main>
          <RouterView></RouterView>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style lang="scss">
html,
body,
#app,
.common-layout,
.el-container{
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
.el-aside{
  background-color: #252B4F;
  h5{
    height: 60px;
    color: white;
    font-size: 20px;
    text-align: center;
    line-height: 60px;
    border: 1px solid white;
}
}

.el-header {
  background-color: #252B4F;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-main{
  background-color: #F5F5F5;
}

.header-actions {
  display: flex;
  justify-content: flex-end;
  padding: 10px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .user-info {
    display: flex;
    align-items: center;
    gap: 10px;

    span {
      color: #409EFF;
    }
  }
}
</style>